﻿using System;
using System.Collections.Generic;

namespace MockNetCore.Handlers
{
    public static class TypeHandlerFactory
    {
        public static Dictionary<Type, TypeHandler> handlerMap = new Dictionary<Type, TypeHandler>();

        static TypeHandlerFactory()
        {
            TypeHandler[] typeHandlers = new TypeHandler[]{
                        new ObjectHandler(),
                        new ArrayHandler(),
                        new StringHandler(),
                        new NumberHandler(),
                        new BooleanHandler()
                };
            foreach (TypeHandler typeHandler in typeHandlers)
            {
                foreach (Type type in typeHandler.support())
                {
                    handlerMap.Add(type, typeHandler);
                }
            }
        }

        /// <summary>
        /// 获取一个类型处理器
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static TypeHandler getTypeHandler(Type type)
        {
            if (!handlerMap.ContainsKey(type))
                throw new Exception("没有指定的类型处理器");
            return handlerMap[type];
        }
    }
}